package com.example.qsindusty.jmm_spline.HomeFragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.qsindusty.jmm_spline.R;
import com.example.qsindusty.jmm_spline.Unallcation.AllcationFragment;
import com.example.qsindusty.jmm_spline.Unallcation.UnAllcationFragment;
import com.example.qsindusty.jmm_spline.recombinationfragment.RecombibationFragment;
import com.yixin.ystartlibrary.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class HomeFragment extends BaseFragment {


    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.home_viewpager)
    ViewPager homeViewpager;
    Unbinder unbinder;

    public HomeFragment() {
        // Required empty public constructor
    }


    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected int getContextView() {
        return 0;
    }

    @Override
    protected void init() {
            initview();
            initViewPager();
    }

    @Override
    protected void initBundleData() {

    }


    private void initview() {
        tablayout.setTabMode(TabLayout.MODE_FIXED);
        tablayout.setTabGravity(Gravity.CENTER);
        tablayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.theme_green));
        tablayout.setupWithViewPager(homeViewpager);
    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment
        ArrayList<Fragment> fragments = new ArrayList<>();// 装填
        fragments.add(UnAllcationFragment.newInstance());
        fragments.add(AllcationFragment.newInstance());
        fragments.add(RecombibationFragment.newInstance());
        // 创建ViewPager适配器
        OrderAdapter myPagerAdapter = new OrderAdapter(this.getChildFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        homeViewpager.setAdapter(myPagerAdapter);
        homeViewpager.setOffscreenPageLimit(3);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public class OrderAdapter extends FragmentPagerAdapter {
        private String[] strings = {"待分配","已分配","复合订单"};
        private List<Fragment> mFragmentList;

        public void setFragments(ArrayList<Fragment> fragments) {
            mFragmentList = fragments;
        }


        public OrderAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            Fragment fragment = mFragmentList.get(position);

            return fragment;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return strings[position];
        }

        @Override
        public int getCount() {

            return mFragmentList.size();
        }

    }


}
